
// ==UserScript==
// @name        xp1024 post viewer parse downloader
// @name:zh-cn  xp1024 帖子预览器 下载地址解析器
// @description    xp1024 帖子预览器 下载地址解析器
// @description:zh-cn xp1024 帖子预览器 下载地址解析器
// @version     1.0
// @author      DaTree
// @namespace   none
// @match       *://*/*
// @match       https://www.b23dowx.xyz/*
// @match       https://xp1024.com/*
// @match       https://z2311k.xyz/*
// @match       https://d0h13c.xyz/*
// @grant       GM_registerMenuCommand
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_download
// @grant       GM_xmlhttpRequest
// @grant       GM_addStyle
// @run-at      document-start
// @license     MIT
// @icon         
// ==/UserScript==


(() => {
  "use strict"
  const utils = {
    /**
     * 参数处理
     * @param {*} params  参数
     */
    tansParams (params) {
      let result = "";
      for (const propName of Object.keys(params)) {
        const value = params[propName];
        var part = encodeURIComponent(propName) + "=";
        if (value !== null && value !== "" && typeof value !== "undefined") {
          if (typeof value === "object") {
            for (const key of Object.keys(value)) {
              if (
                value[key] !== null &&
                value[key] !== "" &&
                typeof value[key] !== "undefined"
              ) {
                let params = propName + "[" + key + "]";
                var subPart = encodeURIComponent(params) + "=";
                result += subPart + encodeURIComponent(value[key]) + "&";
              }
            }
          } else {
            result += part + encodeURIComponent(value) + "&";
          }
        }
      }
      return result;
    },
    wait (func, timespan = 100, timeoutCount = 500) {
      let waitIndex = 0;
      if (!window.__waitId__) {
        window.__waitId__ = 10000;
      }
      const waitId = ++window.__waitId__
      return new Promise((resolve, reject) => {
        const timer = setInterval(() => {
          waitIndex++;
          // console.log(`wait_id:${waitId},wait:${waitIndex}...`)
          if (func()) {
            clearInterval(timer);
            resolve();
          }
          if (waitIndex > timeoutCount) {
            clearInterval(timer);
            console.error(`wait_id:${waitId},wait:timerout!!!`)
            reject();
          }
        }, timespan);
      })
    },
    parseHTMLDOM (htmlString) {
      // 使用DOMParser来解析字符串
      var parser = new DOMParser();
      var doc = parser.parseFromString(htmlString, "text/html");
      return doc;
    }
  }
  const request = {
    send (config) {
      config.method = config.method ? config.method.toUpperCase() : "GET";
      // get请求映射params参数
      if (config.method === "GET" && config.params) {
        let url = config.url + "?" + utils.tansParams(config.params);
        url = url.slice(0, -1);
        config.params = {};
        config.url = url;
      }
      config.headers = config.headers ? config.headers : {
        "Content-Type": "application/json"
      }
      return new Promise((resolve, reject) => {
        GM_xmlhttpRequest({
          method: config.method,
          url: config.url,
          headers: config.headers,
          data: config.data,
          onload: function (response) {
            resolve(response.responseText);
          },
          onerror (e) {
            reject(e);
          }
        });
      });
    },

    get (config) {
      config.method = "GET"
      return this.send(config);
    },
    post (config) {
      config.method = "POST"
      return this.send(config);
    }
  }




  const service = {
    async init () {
      await utils.wait(() => {
        return document.readyState == 'interactive' || document.readyState == "complete"
      });

      this.main();

    },
    initCSS () {
      let css = `
        html,body{
          width:100%;
          height:100%;
        }
        * {
          margin:0;
          padding:0;
        }
        .prev-download-box{
          text-align: left !important;
        }
        `
      GM_addStyle(css)
    },
    async main () {
      const downLoadBox = document.querySelector(".dlboxbg");
      if(downLoadBox){
        downLoadBox.classList.add("prev-download-box");
        document.body.innerHTML = downLoadBox.outerHTML;
        this.initCSS();
        const downBTN1 = document.querySelector("#hash2")
        downBTN1.onclick = null;
        const downBTN2 = document.querySelector("#hash2")
        downBTN2.onclick = null;
      }


    }
  }
  service.init();
})();